#include "dominion.h"
#include "dominion_helpers.h"
#include "rngs.h"
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "testutilities.h"
#include <assert.h>

/*
endTurn()
	parameters - gameState g
	pre-conditions: a valid gameState
		whoseTurn < numPlayers
		currentPlayer = whoseTurn

	post-conditions: 
		if whoseTurn < numPlayers -1
			whoseTurn == whoseTurn+1
		else
			whoseturn == 0

	played hand cards should be in the discard
	if outpost is played, currentPlayer ==whoseTurn
		currentPlayer->handCount == 3
	else
	



*/
int testEndTurn(struct gameState *g){
	int currentPlayer = g->whoseTurn;
	int outpost = g->outpostPlayed;
	assert(currentPlayer < g->numPlayers);
	int hand = g->handCount[g->whoseTurn];
	int discard = g->discardCount[g->whoseTurn];
	int deck = g->deckCount[g->whoseTurn];
	int played = g->playedCardCount;
	int total = hand + discard + deck + played;
	endTurn(g);

	printf("hand: %d discard: %d deck: %d newDiscard: %d newDeckCount: %d newHand: %d\n", hand, discard, deck, g->discardCount[currentPlayer], g->deckCount[currentPlayer], g->handCount[currentPlayer]);

	if(outpost == 1){
		assert(g->whoseTurn == currentPlayer);
		assert(g->handCount[g->whoseTurn] == 3);
		assert(g->outpostPlayed == 0);
	}
	else{
		if(currentPlayer < (g->numPlayers-1)){
			assert(g->whoseTurn == (currentPlayer + 1) && "whoseTurn adjusted incorrectly");
		}
		else{
			assert(g->whoseTurn == 0 && "whoseTurn not reset to zero");
		}
		assert(g->handCount[currentPlayer] == 5 );
	}
	assert(g->outpostPlayed == 0 && "Outpost Not reset");
	if(deck > 4){
		
		assert(g->discardCount[currentPlayer] == (hand + discard) && "hand improperly discarded");
	}
	assert (g->playedCardCount == 0 && "playedCount not reset");
	assert(total == g->handCount[currentPlayer] + g->deckCount[currentPlayer] + g->discardCount[currentPlayer] && "Cards changed");

	return 0;
}

int main(int argc, char** argv){
	int tests = 1000;
	int seed = 1;

	if(argc > 1){
		tests = atoi(argv[1]);
	}
	if(argc > 2){
		seed = atoi(argv[2]);
	}
	prepRandom(1, seed);
	printf("Testing endTurn() tests: %d seed: %d\n", tests, seed);
	int i;

	for(i=0; i<tests; i++){
		struct gameState *g = newGame();
		makeRandomGameState(g);
		testEndTurn(g);
		free(g);
	}

	printf("Test Complete\n");

	return 0;
}
